﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DTTB.Common;
using DTTB.Domain;

namespace DTTB.DAO
{
    public class UsersInRolesDao : AccessData
    {
        //Lấy tất cả UsersInRoles của một User
        public IList<UsersInRoles> GetUsersInRolesByUserId( string userId)
        {
            CommandText = "Select * From TB_UsersInRoles as U Where U.UserId = " + userId +
                          " and U.IsDelete = 0";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            IList<UsersInRoles> list = CollectionHelper.ConvertTo<UsersInRoles>(table);

            return list;
        }

        //Xóa tất cả UsersInRoles của một User
        public bool DeleteAllUserInRolesByUserId(string userId)
        {
            CommandText = "Delete From TB_UsersInRoles Where UserId = " + userId;
            Commandtype = CommandType.Text;

            return Save();
        }

        //Lấy tất cả UsersInRoles theo rolesId
        public IList<UsersInRoles> GetUsersInRolesByRoleId(string roleId)
        {
            CommandText = "Select * From TB_UsersInRoles as U where U.RoleId = " + roleId + " and U.IsDelete = 0";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            IList<UsersInRoles> list = CollectionHelper.ConvertTo<UsersInRoles>(table);
            return list;
        }

        public IList<UsersInRoles> GetAllUsersInRoles()
        {
            CommandText = "Select * From TB_UsersInRoles as U Where U.IsDelete = 0";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            IList<UsersInRoles> list = CollectionHelper.ConvertTo<UsersInRoles>(table);

            return list;
        }

        public bool AddNewUsersInRoles(UsersInRoles usersInRoles)
        {
            return Insert<UsersInRoles>("TB_UsersInRoles", usersInRoles);
        }
    }
}
